package com.hbd.esdemo.model;

import java.io.Serializable;

/**
 * @author bdhong
 */
public class Result<T> implements Serializable {
    private Boolean success;
    private String msg;
    private Integer code;
    private T data;

    private static <T> Result<T> restResult(Boolean success, T data, int code, String msg) {
        Result<T> apiResult = new Result();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setSuccess(success);
        return apiResult;
    }

    public static <T> Result<T> build(Boolean success, String msg, Integer code, T data) {
        return restResult(success, data, code, msg);
    }

    public static <T> Result<T> buildSuccess() {
        return (Result<T>) buildSuccess("操作成功", (Object) null);
    }

    public static <T> Result<T> buildSuccess(Integer code, String msg, T data) {
        return restResult(Boolean.TRUE, data, code, msg);
    }

    public static <T> Result<T> buildSuccess(String msg, T data) {
        return restResult(Boolean.TRUE, data, 0, msg);
    }

    public static <T> Result<T> buildSuccess(String msg) {
        return (Result<T>) buildSuccess(msg, (Object) null);
    }

    public static <T> Result<T> buildSuccess(T data) {
        return buildSuccess("操作成功", data);
    }

    public static <T> Result<T> buildFailure(Integer code, String msg, T data) {
        return restResult(Boolean.FALSE, data, code, msg);
    }

    public static <T> Result<T> buildFailure(String msg, T data) {
        return restResult(Boolean.FALSE, data, 1, msg);
    }

    public static <T> Result<T> buildFailure(Integer code, String msg) {
        return (Result<T>) buildFailure(code, msg, (Object) null);
    }

    public static <T> Result<T> buildFailure(String msg) {
        return (Result<T>) buildFailure((String) msg, (Object) null);
    }

    public static <T> Result<T> buildFailure(T data) {
        return buildFailure("", data);
    }

    public static <T> ResultBuilder<T> builder() {
        return new ResultBuilder();
    }

    @Override
    public String toString() {
        return "Result(success=" + this.getSuccess() + ", msg=" + this.getMsg() + ", code=" + this.getCode() + ", data=" + this.getData() + ")";
    }

    public Result() {
    }

    public Result(Boolean success, String msg, Integer code, T data) {
        this.success = success;
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public Boolean getSuccess() {
        return this.success;
    }

    public Result<T> setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Integer getCode() {
        return this.code;
    }

    public Result<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static class ResultBuilder<T> {
        private Boolean success;
        private String msg;
        private Integer code;
        private T data;

        ResultBuilder() {
        }

        public ResultBuilder<T> success(Boolean success) {
            this.success = success;
            return this;
        }

        public ResultBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public ResultBuilder<T> code(Integer code) {
            this.code = code;
            return this;
        }

        public ResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public Result<T> build() {
            return new Result(this.success, this.msg, this.code, this.data);
        }

        @Override
        public String toString() {
            return "Result.ResultBuilder(success=" + this.success + ", msg=" + this.msg + ", code=" + this.code + ", data=" + this.data + ")";
        }
    }
}
